package com.miplus.ccrm.core.service.crd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.core.controller.vo.credit.AiListResVo;
import com.miplus.ccrm.core.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class HttpCommand {

    public static int SendAiList(String tempid, List<AiListResVo> listResVos)
    { //"busiType" 乐分144   美团车贷145
        List<String> formattedStrings = null;
        JSONObject jsonObject = new JSONObject();
        if(JPubUtils.IsEqual(tempid,"ailist-ys")) {
            formattedStrings = listResVos.stream()
                    .map(mt -> mt.getDataId() + "|" + mt.getAcctType() +"|" + mt.getCustname()+"|" + mt.getGender()+ "|" + mt.getCustNo() + "|" + mt.getMobileNumber() + "|" + mt.getCardNo() + "|" + mt.getMthsOdue() + "|" + mt.getBillDay() + "|" + mt.getBillMonth() + "|" + JPubUtils.RTrimZero(mt.getOdBal()) + "|" + JPubUtils.RTrimZero(mt.getOverdueAmt()) + "|" + JPubUtils.RTrimZero(mt.getLowestRepayBal()) + "|" + JPubUtils.RTrimZero(mt.getPaidMoneyAmt()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillAmt()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillInt()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillFee()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillPrin()) + "|" + mt.getOpenDt() + "|" + JPubUtils.RTrimZero(mt.getBillTotalBal())
                            + "|" + mt.getCardNo2() + "|" + mt.getMthsOdue2() + "|" + mt.getBillDay2() + "|" + mt.getBillMonth2() + "|" + JPubUtils.RTrimZero(mt.getOdBal2()) + "|" + JPubUtils.RTrimZero(mt.getOverdueAmt2()) + "|" + JPubUtils.RTrimZero(mt.getLowestRepayBal2()) + "|" + JPubUtils.RTrimZero(mt.getPaidMoneyAmt2()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillAmt2())+ "|" + JPubUtils.RTrimZero(mt.getSettledBillInt2()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillFee2()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillPrin2()) + "|" + mt.getOpenDt2() + "|" + mt.getBillTotalBal2() + "|" + mt.getIsUrgeCust() + "|" + mt.getDataDt())
                    .collect(Collectors.toList());
            jsonObject.put("busiType", "144");
        }
        else{
            formattedStrings = listResVos.stream()
                    .map(mt -> mt.getDataId() + "|" + mt.getAcctType() + "|"+mt.getCustname()+"|" + mt.getGender()+ "|"+ mt.getCustNo() + "|" + mt.getMobileNumber() + "|" + mt.getCardNo() + "|" + mt.getMthsOdue() + "|" + mt.getBillDay() + "|" + mt.getBillMonth() + "|" + JPubUtils.RTrimZero(mt.getOdBal())+ "|" + JPubUtils.RTrimZero(mt.getOverdueAmt()) + "|" + JPubUtils.RTrimZero(mt.getLowestRepayBal()) + "|" + JPubUtils.RTrimZero(mt.getPaidMoneyAmt()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillAmt()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillInt()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillFee()) + "|" + JPubUtils.RTrimZero(mt.getSettledBillPrin()) + "|" + mt.getOpenDt() + "|" + JPubUtils.RTrimZero(mt.getBillTotalBal())
                            + "|" + mt.getIsUrgeCust() + "|" + mt.getDataDt())
                    .collect(Collectors.toList());
            jsonObject.put("busiType", "145");
        }

        //将 List 转换为 JSONArray
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(formattedStrings);
        jsonObject.put("content", jsonArray);

        JSONObject bodyObject = new JSONObject();
        bodyObject.put("appCode","D1002T");
        bodyObject.put("body",jsonObject);

        String httpBody = bodyObject.toJSONString();
        log.info("SendAiList:" + httpBody);

        return sendAiPush(httpBody);
    }

    public static String getAiPushUrl()
    {
        Environment environment = (Environment) SpringContextUtil.getBean("environment");
        String cmdurl = environment.getProperty("credit.aipush.url");
        if (JPubUtils.IsEmpty(cmdurl)) {
            throw new ServiceException(EnumRCode.NoFindConfig.value, EnumRCode.NoFindConfig.name);
        }
        return cmdurl;
    }

    private static int sendAiPush(String httpBody) {

        String aiurl=getAiPushUrl();
        log.info("sendAiPush:{}", aiurl);
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest.post(aiurl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(httpBody)
                    .asJson();
            // 获取响应体
            log.info("sendAiPush.Response:{}", jsonResponse);
            int status = jsonResponse.getStatus();
            if (status == 200) {
                JsonNode responseBody = jsonResponse.getBody();
                String body=responseBody.getObject().get("body").toString();
                log.info("sendAiPush.ResponseBody:{}", body);

                JSONObject bodyObject = JSONObject.parseObject(body);

                String retCode=bodyObject.get("retCode").toString().toUpperCase();
                String retMsg=bodyObject.get("retMsg").toString();
                if(!JPubUtils.IsEqual(retCode,"SUCCESS")){
                    throw new ServiceException(EnumRCode.CmdFail.value, retMsg);
                }
            }
            return 0;
        } catch (Exception e) {
            JPubUtils.PrintError("sendAiPush",e);
            throw new ServiceException(EnumRCode.Exception.value, e.getMessage());
        }
    }
}
